package com.easywan.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.easywan.dao.ArticleMapper;
import com.easywan.dao.IBaseDao;
import com.easywan.service.ArticleService;
import com.easywan.vo.Article;
import com.easywan.vo.Users;
import com.easywan.vo.base.BaseModel;

@Service
public class ArticleServiceImpl extends BaseServiceImpl implements ArticleService{
	@Autowired
	ArticleMapper articleMapper;
	
	
	@Override
	public int addArticle(Article article, Users userInfo) {
		article.setUid(userInfo.getId());
		Date nowTime = new Date(System.currentTimeMillis());
		article.setCreatetime(nowTime);
		article.setModifytime(nowTime);
		return articleMapper.insertSelective(article);
	}

	@Override
	public Article getArticleById(Integer id) {
		
		return articleMapper.selectByPrimaryColumn(id);
	}

	@Override
	public List<Article> selectLast(int i) {
		
		return articleMapper.selectByLimit(0,i);
	}

	@Override
	public Article reads(Integer id, Users userInfo) {
		Article result = articleMapper.selectByPrimaryColumn(id);
		if(userInfo == null ||  userInfo.getId() != result.getId()){
			result.setLooks(result.getLooks()+1);
			articleMapper.updateByPrimaryColumn(result);
		}
		return result;
	}

	@Override
	public List<Article> selectByPage(Article tmpArticle) {
		if(null == tmpArticle.getPage()){
			return articleMapper.select(tmpArticle);
		}
		return articleMapper.selectByPage(tmpArticle);
	}
	@Override
	public <T extends BaseModel> void modify(T loadFiled) {
		((Article)loadFiled).setFlags(true);
		super.modify(loadFiled);
	}

	@Override
	public IBaseDao getMapper() {
		// TODO Auto-generated method stub
		return this.articleMapper;
	}
	
}
